package com.health.service.Impl;





import com.alibaba.fastjson2.JSON;
import com.health.dto.UserLoginDTO;
import com.health.entity.Users;
import com.health.mapper.UserMapper;
import com.health.properties.JwtProperties;
import com.health.properties.WeChatProperties;
import com.health.result.OpenIdResult;
import com.health.service.UserService;
import com.health.utils.HttpClientUtil;
import com.health.utils.JwtUtil;
import com.health.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private WeChatProperties weChatProperties;


    @Override
    public UserLoginVO login(String code) {
        UserLoginVO vo = new UserLoginVO();
//        vo.setOpenId("1619193086620028928");

        Map<String, String> getOpenid = new HashMap<>();
        getOpenid.put("appid", weChatProperties.getAppid());
        getOpenid.put("secret", weChatProperties.getSecret());
        getOpenid.put("js_code", code);
        getOpenid.put("grant_type", "authorization_code");

        log.info("=========getPoenid{}",getOpenid.toString());

        String result = HttpClientUtil.doGet("http://localhost:9090/sns/jscode2session", getOpenid);
        OpenIdResult openIdResult = JSON.parseObject(result, OpenIdResult.class);
        String Openid = openIdResult.getOpenid();
        log.info("===openid :{}",Openid);

        vo.setOpenId(Openid);
        return vo;
    }

    @Override
    @Transactional
    public UserLoginVO register(UserLoginDTO userLoginDTO) {
        //注意：测试号获得不了用户openId
        //判断:因为opendid是微信用户的唯一标识. 根据openid去数据库查询用户是否注册过
        Users user = userMapper.selectUserById(userLoginDTO.getPhone());
        //判断手机号是否存在于数据库
        if (user == null) {
            Users u = new Users();
            BeanUtils.copyProperties(userLoginDTO,u);
            u.setNickname("小河河");
            //添加用户信息到数据库中
            userMapper.add(u);
        }

        // 开发者服务端，自定义登录态，生成令牌(token)openid等数据返回给小程序端，方使后结清求身份校验
        Map<String, Object> claim = new HashMap<>();
        claim.put("phone", userLoginDTO.getPhone());
        //创建令牌，设置token的过期时间
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claim);
        //将token设置到redis数据库中
        redisTemplate.opsForValue().set(jwtProperties.getUserTokenName() + token, user.getId(), jwtProperties.getUserTtl(), TimeUnit.MILLISECONDS);
        //3.1封装数据返回给前端
        UserLoginVO vo = new UserLoginVO();
        vo.setToken(token);
        return vo;
    }
}
